package com.shipwe.loans.ui.adapter

import android.annotation.SuppressLint
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.shipwe.adapter.ShipweRecyclerViewAdapter
import com.shipwe.adapter.ShipweViewHolderHelper
import com.shipwe.loans.R
import org.json.JSONObject

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/25 14:42
 * @Annotation：贷款申请 明细 适配器
 */
class ApplyDetailsAdapter(recyclerView: RecyclerView?) : ShipweRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.apply_details_title_item) {
    override fun setItemChildListener(helper: ShipweViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.detailsTitleLayout) //点击Item
    }

    @SuppressLint("SetTextI18n")
    override fun fillData(helper: ShipweViewHolderHelper, position: Int, model: JSONObject) {
        val detailsTitle = helper.getTextView(R.id.detailsTitle)
        detailsTitle.text = model.optString("showName")

        val detailsValue = helper.getTextView(R.id.detailsValue)
        detailsValue.text = model.optString("showDetail")

        val childArray = model.optJSONArray("childList")
        val childList = arrayListOf<JSONObject>()
        if (childArray != null) {
            for (i in 0..<childArray.length()){
                childList.add(childArray.optJSONObject(i))
            }
        }

        val detailsDescListView = helper.getView<RecyclerView>(R.id.detailsDescListView)
        detailsDescListView.setHasFixedSize(true)
        detailsDescListView.isNestedScrollingEnabled = false
        detailsDescListView.isFocusableInTouchMode = false //不需要焦点
        detailsDescListView.layoutManager = LinearLayoutManager(mContext)
        val adapter = ApplyDetailsDescAdapter(detailsDescListView)
        detailsDescListView.adapter = adapter
        detailsDescListView.itemAnimator = DefaultItemAnimator()
        adapter.data = childList
    }

    class ApplyDetailsDescAdapter(recyclerView: RecyclerView?) : ShipweRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.apply_details_desc_item) {
        @SuppressLint("SetTextI18n")
        override fun fillData(helper: ShipweViewHolderHelper, position: Int, model: JSONObject) {
            val detailsDescTitle = helper.getTextView(R.id.detailsDescTitle)
            detailsDescTitle.text = model.optString("showName")

            val detailsDescValue = helper.getTextView(R.id.detailsDescValue)
            detailsDescValue.text = model.optString("showDetail")
        }
    }
}